package com.rs.lc.pm.model.utils;
/*
* Copyright (c) 2015,Deepspring Inc. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This product is protected by copyright and distributed under
* licenses restricting copying, distribution, and decompilation.
*/

import com.alibaba.fastjson.JSON;

import java.io.Serializable;


public class JsonResponse implements Serializable {

    private static final long serialVersionUID = 4029305952742606795L;
    public static final int STATUS_OK = 200;

    public static final int STATUS_BAD_REQUEST = 400;

    public static final int STATUS_FORBIDDEN_REQUEST = 403;


    /**
     * 生成默认的200 正确返回对象
     */
    public JsonResponse() {
        this.status = STATUS_OK;
    }

    public JsonResponse(int status, String errorMsg) {
        this.status = status;
        this.errorMsg = errorMsg;
    }

    public JsonResponse(Object data) {
        this.status = STATUS_OK;
        this.data = data;
    }

    public JsonResponse(int status, Page page, Object data) {
        this.status = status;
        this.page = page;
        this.data = data;
    }

    public static JsonResponse create() {
        return new JsonResponse();
    }

    /**
     * 添加data数据
     *
     * @param data
     * @return
     */
    public JsonResponse addData(Object data) {
        this.data = data;
        return this;
    }

    /**
     * 添加page数据
     *
     * @param page
     * @return
     */
    public JsonResponse addPage(Page page) {
        this.page = page;
        return this;
    }


    private int status;// 200, 正常. 非200, 不正常.     600开头都是用户输入等问题,  500开头都是服务器内部问题
    private String errorMsg; // 错误提示信息
    private Object data;// 数据信息
    private Page page;// 分页信息


    public int getStatus() {
        return status;
    }

    public JsonResponse setStatus(int status) {
        this.status = status;
        return this;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Page getPage() {
        return page;
    }

    /**
     * TODO 后台可用，app以后使用时间戳作为分页工具
     *
     * @param page
     * @return
     */
    public JsonResponse setPage(Page page) {
        this.page = page;
        return this;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("CrmJsonResponse");
        sb.append("{status=").append(status);
        sb.append(", errorMsg='").append(errorMsg).append('\'');
        sb.append(", data=").append(data);
        sb.append(", page=").append(page);
        sb.append('}');
        return sb.toString();
    }

    /**
     * 使用fastjson的json转换，日期=毫秒
     *
     * @return
     */
    public String toJsonString() {
        return JSON.toJSONString(this);
    }
}

